package com.pay.game.central.utils.myIdGen;

import com.pay.game.central.utils.myIdGen.exception.MaxIDException;

public class MyIDGenerator {

    public static final String FIRST_ID = "0000";

    public static final String[] AZaz09 = {
            "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
            "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
            "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
            "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    };

    public static String getNextId(String currentId) throws MaxIDException {
        if (currentId.equals("zzzz")){
            throw new MaxIDException();
        }
        char c0 = currentId.charAt(0);
        char c1 = currentId.charAt(1);
        char c2 = currentId.charAt(2);
        char c3 = currentId.charAt(3);
        //获取index
        int i0 = AZaz09[0].indexOf(c0);
        int i1 = AZaz09[0].indexOf(c1);
        int i2 = AZaz09[0].indexOf(c2);
        int i3 = AZaz09[0].indexOf(c3);
        i3 ++;
        if (i3 == 64){
            i3 = 0;
            i2++;
            if (i2 == 64){
                i2 = 0;
                i1 ++;
                if (i1 == 64){
                    i1 = 0;
                    i0 ++;
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        sb.append(AZaz09[0].charAt(i0));
        sb.append(AZaz09[1].charAt(i1));
        sb.append(AZaz09[2].charAt(i2));
        sb.append(AZaz09[3].charAt(i3));
        return sb.toString();

    }

    public static String getFirstId(){
        return  FIRST_ID;
    }

}
